package com.hb.software.controller.user;

import com.alibaba.fastjson.JSON;
import com.hb.software.entity.user.User;
import com.hb.software.user.UserService;
import com.hb.software.utils.CreateImageCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("login")
@CrossOrigin //允许跨域
@Slf4j
public class LoginController {
    private static final Logger logger = LogManager.getLogger(LoginController.class.getName());
    @Autowired
    private UserService userService;

    /**
     * 生成验证码
     *
     * @throws IOException
     */
    @GetMapping("getImage")
    public Map<String, String> getImage(HttpServletRequest request) throws IOException {
        Map<String, String> result = new HashMap<>();
        CreateImageCode createImageCode = new CreateImageCode();
        //获取验证码
        String securityCode = createImageCode.getCode();
        //验证码存入session
        String key = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        request.getServletContext().setAttribute(key, securityCode);
        //生成图片
        BufferedImage image = createImageCode.getBuffImg();
        //进行base64编码
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", bos);
        String string = Base64Utils.encodeToString(bos.toByteArray());
        result.put("key", key);
        result.put("image", string);
        result.put("true_code", securityCode);
        logger.info(securityCode);
        return result;
    }

    @PostMapping("/findById")
    public User findById(@RequestBody String id){
        return userService.findById(Long.valueOf(id.substring(0,id.length()-1)));
    }
    @PostMapping("/update")
    public void update(@RequestBody User user){
        userService.update(user);
    }

    @PostMapping("/delete")
    public void delete(@RequestBody String id){
        userService.delete(Long.valueOf(id.substring(0,id.length()-1)));
    }

    @RequestMapping("/login")
    public String login(@RequestBody User user){
        logger.info(user);
        String flag = "error";
        User us = new User();
        try{
            us = userService.login(user);
        }catch (Exception e){
            us=null;
        }
        logger.info(us);
        HashMap<String,Object> res = new HashMap<>();
        if(us!=null){
            flag="ok";
        }
        res.put("flag",flag);
        res.put("user",us);
        String res_json = JSON.toJSONString(res);
        return res_json;

    }


}
